package org.example;

import java.sql.*;

public class TestMaven2 {
    public static void main(String[] args)throws Exception {
        System.out.println("保存用户信息：");
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_maven",
                "root","");
            //保存数据
            String username = "鹿山学院";
            int age = 18;
            java.util.Date birth = new java.util.Date();
            String sql = "insert into user(username,age,birth) values(?,?,?)";
            //编译sql语句
            PreparedStatement pstmt = conn.prepareStatement(sql);
            //给占位符赋值
            pstmt.setString(1,username);
            pstmt.setInt(2,age);
            pstmt.setObject(3,birth);
            //执行sql语句
            pstmt.executeUpdate();

            pstmt = conn.prepareStatement("select * from user");


            ResultSet rs = pstmt.executeQuery();

            while(rs.next()){
                System.out.print(rs.getInt("id")+"\t");
                System.out.print(rs.getString("username")+"\t");
                System.out.print(rs.getInt("age")+"\t");
                System.out.print(rs.getTimestamp("birth")+"\t");
                System.out.println();
            }
            rs.close();
            conn.close();



}
}
